package com.xtourms.client.rm;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.Context;
import com.xtourms.client.rm.hotel.HotelManagerFrame;
import com.xtourms.client.rm.product.ProductManagerFrame;
import com.xtourms.client.rm.ticket.TicketManagerFrame;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.shared.field.WorkerField;

public class ResManagerFrame extends Composite {

	private static ResManagerUiBinder uiBinder = GWT
			.create(ResManagerUiBinder.class);

	interface ResManagerUiBinder extends UiBinder<Widget, ResManagerFrame> {
	}
	
	private Widget cur_res;
	private Anchor cur_nav;
	@UiField Anchor res_hotel;
	@UiField Anchor res_ticket;
	//@UiField Anchor res_travel;
	@UiField Anchor res_product;
	@UiField HTMLPanel resManager;
	@UiField HTMLPanel nothing;
	
	public ResManagerFrame() {
		initWidget(uiBinder.createAndBindUi(this));
		cur_res = nothing;
		initFrame();
	}
	
	private void initFrame() {
		int droit = (int)((JSONNumber)Context.ins.worker.get(WorkerField.WORKER_DROIT)).doubleValue();
		switch(droit){
		case WorkerField.SYS_DROIT:
		case WorkerField.MANAGER_DROIT:
			switchRM(res_hotel, new HotelManagerFrame());
			break;
		case WorkerField.OPERATOR_DROIT:
			//FIXME:关闭其他管理权限
			res_hotel.setVisible(false);
			res_ticket.setVisible(false);
			res_product.setVisible(false);
			MessageBox.ins.showWarning("您不具备管理员权限，请联系管理员。");
			break;
		default:
		}
	}

	private void switchRM(Anchor nextNav, Widget nextRm) {
		if(cur_nav != nextNav){
			if(null != cur_nav){
				cur_nav.removeStyleName("cur_sub_nav");
			}
			cur_nav = nextNav;
			cur_nav.addStyleName("cur_sub_nav");
			cur_res.removeFromParent();
			cur_res = nextRm;
			resManager.add(cur_res, "res_content");
		}
	}
	
	@UiHandler("res_hotel")
	void manageHotel(ClickEvent evt){
		switchRM(res_hotel, new HotelManagerFrame());
	}
	
	@UiHandler("res_ticket")
	void manageTicket(ClickEvent evt){
		switchRM(res_ticket, new TicketManagerFrame());
	}
	
//	@UiHandler("res_travel")
//	void manageTravel(ClickEvent evt){
//		switchRM(res_travel, new TravelManagerFrame());
//	}
	
	@UiHandler("res_product")
	void manageProduct(ClickEvent evt){
		switchRM(res_product, new ProductManagerFrame());
	}
}
